const App = getApp()
//const api = require('../../utils/api.js');

Page({
    data: {
        modalHidden: true,
        goods: { item: ['a', 'b', 'c', 'd', 'e'] },
        firstIndex: 0,
        secondIndex: 0,
        firstList:[],
        secondList: [],
        order: {},
        prompt: {
            hidden: !0,
            icon: '../../../assets/images/iconfont-order-default.png',
            title: '您还没有相关的订单',
            text: '可以去看看有哪些想买的',
        },
    },
    onLoad() {
      this.order = App.HttpResource('/order/:id', {id: '@id'})
        // this.order = App.HttpResource('https://www.skyvow.cn/api/order/:id', { id: '@id' })
        this.setData({
            firstList: [
                  {
                    name: '淘淘领',
                    _id: 'taotaoling',
                  },
                  {
                    name: '宅急送',
                    _id: 'zhaijisong',
                  }
              ],
            secondList: [
                  {
                      name: '全部',
                      _id: 'all',
                  },
                  {
                      name: '待付款',
                      _id: 'submitted',
                  },
                  {
                      name: '待返款',
                      _id: 'confirmed',
                  },
                  {
                      name: '已反款',
                      _id: 'finished',
                  },
                  {
                      name: '已取消',
                      _id: 'canceled',
                  }
            ]
        })
        this.onPullDownRefresh()
    },
    initData() {
        const order = this.data.order
        const params = order && order.params
        const type = params && params.type || 'all'

        this.setData({
            order: {
                items: [],
                params: {
                    page : 1,
                    limit: 10,
                    type : type,
                },
                paginate: {}
            }
        })
    },
    navigateTo(e) {
        console.log(e)
        App.WxService.navigateTo('/pages/order/detail/index', {
            id: e.currentTarget.dataset.id
        })
    },

    //页面弹出半透明窗口js
    modalTap: function (e) {
      this.setData({
        modalHidden: false
      })
    },

    modalChange: function (e) {
      this.setData({
        modalHidden: true
      })
    },
    modalChange1: function (e) {
      this.setData({
        modalHidden: true
      })
      //跳转到已付款状态
      wx.navigateTo({
        url: '../orderlist/orderlist'
      })
    },

    getList() {
        const order = this.data.order
        const params = order.params

        // App.HttpService.getOrderList(params)
        this.order.queryAsync(params)
        .then(res => {
            const data = res.data
            console.log(data)
            if (data.meta.code == 0) {
                order.items = [...order.items, ...data.data.items]
                order.paginate = data.data.paginate
                order.params.page = data.data.paginate.next
                order.params.limit = data.data.paginate.perPage
                this.setData({
                    order: order,
                    'prompt.hidden': order.items.length,
                })
            }
        })
    },
    onPullDownRefresh() {
        console.info('onPullDownRefresh')
        this.initData()
        //this.getList()
    },
    onReachBottom() {
        console.info('onReachBottom')
        if (!this.data.order.paginate.hasNext) return
        this.getList()
    },

    //tab标签的互动切换
    onTapTag_First(e) {
      const type = e.currentTarget.dataset.type
      const index = e.currentTarget.dataset.index
      this.initData()
      this.setData({
        firstIndex: index,
        //二级tab归零
        secondIndex: 0,
        'order.params.type': type,
      })
      this.getList()
    },
    onTapTag_Second(e) {
        const type = e.currentTarget.dataset.type
        const index = e.currentTarget.dataset.index
        this.initData()
        this.setData({
             secondIndex: index,
            'order.params.type': type,
        })
        this.getList()
    },
})